package translateport;

import com.fengwei.util.TransPortData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       translateport.TranslatePort.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/8      feiwe                         add
 */


public class TranslatePort {
    private final static Logger log = LoggerFactory.getLogger(TranslatePort.class);

    public static void main(String[] args) {
        try {
            //获取本地监听端口、远程IP和远程端口
            int localPort = 3300;
            String remoteIp = "101.231.60.125";
            int remotePort = 10038;

            //启动本地监听端口
            ServerSocket serverSocket = new ServerSocket(localPort);
            log.error("localPort=" + localPort + ";remoteIp=" + remoteIp +
                    ";remotePort=" + remotePort + ";启动本地监听端口" + localPort + "成功！");

            while (true) {
                Socket clientSocket = null;
                Socket remoteServerSocket = null;
                try {
                    //获取客户端连接
                    clientSocket = serverSocket.accept();

                    log.error("accept one client");
                    //建立远程连接
                    remoteServerSocket = new Socket(remoteIp, remotePort);

                    log.error("create remoteip and port success");
                    //启动数据转换接口
                    (new TransPortData(clientSocket, remoteServerSocket, "1")).start();
                    (new TransPortData(remoteServerSocket, clientSocket, "2")).start();
                } catch (Exception ex) {
                    log.error("", ex);
                }
                //建立连接远程
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
